package com.jshop.admin.sys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jshop.admin.sys.dao.SysRoleDao;
import com.jshop.admin.sys.entity.SysRoleEntity;
import com.jshop.admin.sys.service.SysRoleDeptService;
import com.jshop.admin.sys.service.SysRoleMenuService;
import com.jshop.admin.sys.service.SysRoleService;
import com.jshop.admin.sys.service.SysUserService;
import com.jshop.common.constant.AdminConstant;
import com.jshop.common.exception.ApiRRException;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * 角色
 */
@Service("sysRoleService")
public class SysRoleServiceImpl extends ServiceImpl<SysRoleDao, SysRoleEntity> implements SysRoleService {
    @Autowired
    private SysRoleDao sysRoleDao;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @Autowired
    @Lazy
    private SysUserService sysUserService;
    @Autowired
    private SysRoleDeptService sysRoleDeptService;


    @Override
    @Transactional
    public void insertInto(SysRoleEntity role) {
        role.setCreateTime(new Date());
        sysRoleDao.insert(role);
        System.out.println(role.toString());
        //检查权限是否越权
        checkPrems(role);

        //保存角色与菜单关系
        sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());

//        保存角色与部门关系
//        sysRoleDeptService.saveOrUpdate(role.getRoleId(), role.getDeptIdList());
    }

    @Override
    @Transactional
    public void updateInfo(SysRoleEntity role) {
        sysRoleDao.updateById(role);
        //检查权限是否越权
        checkPrems(role);
        sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());
    }

    @Override
    @Transactional
    public void deleteBatch(Integer[] roleIds) {
        sysRoleDao.deleteBatchIds(Arrays.asList(roleIds));
    }

    @Override
    public List<Integer> queryRoleIdList(Integer createUserId) {
        return sysRoleDao.queryRoleIdList(createUserId);
    }

    @Override
    public List<SysRoleEntity> queryPublicRole() {
        return sysRoleDao.queryPublicRole();
    }

//    @Override
//    public Page<UserWindowDto> queryPageByDto(UserWindowDto userWindowDto, int pageNmu) {
//        return null;
//    }

    /**
     * 检查权限是否越权
     */
    private void checkPrems(SysRoleEntity role) {
        //如果不是超级管理员，则需要判断角色的权限是否超过自己的权限
        if (role.getCreateUserId() == AdminConstant.SUPER_ADMIN) {
            return;
        }

        //查询用户所拥有的菜单列表
        List<Integer> menuIdList = sysUserService.queryAllMenuId(role.getCreateUserId());

        //判断是否越权
        if (!menuIdList.containsAll(role.getMenuIdList())) {
            throw new ApiRRException("新增角色的权限，已超出你的权限范围");
        }
    }

//    @Override
//    public Page<UserWindowDto> queryPageByDto(UserWindowDto userWindowDto, int pageNum) {
//        PageHelper.startPage(pageNum, Constant.pageSize);
//        sysRoleDao.queryPageByDto(userWindowDto);
//        return PageHelper.endPage();
//    }
}
